<?php
//Knowledge Management Controller By Teerapong Singthong.
class Blog_model extends Model{
	function __construct(){
		parent::Model();
	}
	
	function setdata(){
		$data["title"] = "Blog.";	//set title.
		$data["topic"] = "Blog - Knowledge Management.";
		$data["navigator"] = "&nbsp;&#8250;&nbsp;Blog Node";
		return $data;	//return $data -> main funct.
	}
	
	function add_blog_category($data){
		$category_name = $this->input->post('category_name');
		$category_description = $this->input->post('category_desc');
		if($category_display =  $data['raw_name']."_thumb".$data['file_ext']=="_thumb"){
			$category_display = "_thumb.png";
		}
		else
		{
			$category_display =  $data['raw_name']."_thumb".$data['file_ext'];
		}
		
		$data = array(
			'category_name' => $category_name,
			'category_description' => $category_description,
			'category_display' => $category_display
		);
		
		$this->db->insert('blog_category',$data);
		
		$query = $this->db->affected_rows();
		
		if($query){
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function edit_blog_category($data){
		$category_name = $this->input->post('category_name');
		$category_description = $this->input->post('category_desc');
		$b_category_id = $this->input->post('b_category_id');
		$category_display =  $data['raw_name']."_thumb".$data['file_ext'];
		
		$data = array(
			'category_name' => $category_name,
			'category_description' => $category_description,
			'category_display' => $category_display
		);
		
		$this->db->where('b_category_id',$b_category_id);
		$this->db->update('blog_category',$data);
		
		$query = $this->db->affected_rows();
		
		if($query){
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function add_entry(){
		$blog_entry_name = $this->input->post('blog_entry_name');
		
		$blog_content = $this->input->post('content_2');
		$postedValue = htmlspecialchars($blog_content);
		
		$blog_author = $this->session->userdata('member_id');
		$b_category_id = $this->input->post('b_category_id');
		
		$date_format = "%Y-%m-%d %h:%i:%a";
		$blog_create_date = mdate($date_format);
		
		$data = array(
			'blog_entry_name' => $blog_entry_name,
			'blog_content' => $postedValue,
			'blog_author' => $blog_author,
			'b_category_id' => $b_category_id,
			'blog_create_date' => $blog_create_date
		);
		
		$this->db->insert('blog',$data);
		$query = $this->db->affected_rows();
		
		if($query){
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function add_comment(){
		$comment = $this->input->post('content_3');
		$comment_details = htmlspecialchars($comment);
		
		$date_format = "%Y-%m-%d %h:%i:%a";
		$comment_create_date = mdate($date_format);
		
		$data = array(
			'member_id' => $this->session->userdata('member_id'),
			'blog_id' => $this->input->post('blog_id'),
			'comment_details' => $comment_details,
			'date_posted' => $comment_create_date
		);
		
		$this->db->insert('blog_comment',$data);
		$query  = $this->db->affected_rows();
		
		if($query){
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function vote_up(){
		/*check unique*/
		$member_id = $this->input->post('member_id');
		$blog_id = $this->input->post('blog_id');
		
		$this->db->where('blog_id',$blog_id);
		$this->db->where('member_id',$member_id);
		$query = $this->db->get('blog_vote');
		
		$num_rows = $query->num_rows();
		
		if($num_rows==0){
			$date_format = "%Y-%m-%d %h:%i:%a";
			$date_voted = mdate($date_format);
			
			$data = array(
				'vote_date' => $date_voted,
				'member_id' => $member_id,
				'like_vote' => 1,
				'dislike_vote' => 0,
				'blog_id' => $blog_id	
			);
			$this->db->insert('blog_vote',$data);
			$is_success = $this->db->affected_rows();
			
			if($is_success){
				return true;
			}
			else
			{
				return false;
			}
		}
		else if($num_rows>0){
			return false;
		}	
	}
	
	function vote_down(){
		/*check unique*/
		$member_id = $this->input->post('member_id');
		$blog_id = $this->input->post('blog_id');
		
		$this->db->where('blog_id',$blog_id);
		$this->db->where('member_id',$member_id);
		$query = $this->db->get('blog_vote');
		
		$num_rows = $query->num_rows();
		
		if($num_rows==0){
			$date_format = "%Y-%m-%d %h:%i:%a";
			$date_voted = mdate($date_format);
			
			$data = array(
				'vote_date' => $date_voted,
				'member_id' => $member_id,
				'like_vote' => 0,
				'dislike_vote' => 1,
				'blog_id' => $blog_id	
			);
			$this->db->insert('blog_vote',$data);
			$is_success = $this->db->affected_rows();
			
			if($is_success){
				return true;
			}
			else
			{
				return false;
			}
		}
		else if($num_rows>0){
			return false;
		}	
	}
	
	function add_views($id){
		$is_logged_in = $this->session->userdata('is_logged_in');
		if(($is_logged_in==true)){
		//check unique
		 $member_id = $this->session->userdata('member_id');
		 $blog_id = $id['id'];
		 
		 $this->db->where('member_id',$member_id);
		 $this->db->where('blog_id',$blog_id);
		 $query = $this->db->get('blog_views_log');
		 
		 $num_rows = $query->num_rows();
		 
		 if($num_rows==0){
			 if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
			 {
				$ip=$_SERVER['HTTP_CLIENT_IP'];
			 }
			 else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
			 {
				$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
			 }
			 else
			 {
				$ip=$_SERVER['REMOTE_ADDR'];
			 }
	
			$date_format = "%Y-%m-%d %h:%i:%a";
			$date_view = mdate($date_format);
			
			$data = array(
				'view_counts' => 1,
				'blog_id' => $blog_id,
				'member_id' => $this->session->userdata('member_id'),
				'view_date' => $date_view,
				'ip_address' => $ip,
				'browser_info' => $_SERVER['HTTP_USER_AGENT']
			);
			
			$this->db->insert('blog_views_log',$data);
			
			$is_success = $this->db->affected_rows();
			if($is_success){
				return true;
			}else{
				return false;
			}
		}
		else{
			return true;
		}
	}
	else{
		return true;
	}
   }
   
   function delete_comment($data){
		$this->db->where('comment_id',$data['id']);
		$this->db->delete('blog_comment');
		$query = $this->db->affected_rows();
		
		if($query){
			return true;
		}
		else
		{
			return true;
		}
   }
   
   function update_entry(){
		$blog_entry_name = $this->input->post('blog_entry_name');
		
		$blog_content = $this->input->post('content_2');
		$postedValue = htmlspecialchars($blog_content);
		
		$blog_author = $this->session->userdata('member_id');
		$b_category_id = $this->input->post('b_category_id');
		
		$date_format = "%Y-%m-%d %h:%i:%a";
		$blog_create_date = mdate($date_format);
		
		$data = array(
			'blog_entry_name' => $blog_entry_name,
			'blog_content' => $postedValue,
			'blog_author' => $blog_author,
			'blog_create_date' => $blog_create_date
		);
		
		$this->db->where('blog_id',$this->input->post('blog_id'));
		$this->db->update('blog',$data);
		$query = $this->db->affected_rows();
		
		if($query){
			return true;
		}
		else
		{
			return false;
		}
	}
   
}